package com.qeegoo.b2bautozityp.moudle.detail.view;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.DragEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.api.entity.HttpResult;
import com.qeegoo.b2bautozityp.moudle.detail.adapter.LeftAdapter;
import com.qeegoo.b2bautozityp.moudle.detail.adapter.SlideApapter;
import com.qeegoo.b2bautozityp.moudle.detail.bean.DataSet;
import com.qeegoo.b2bautozityp.moudle.detail.bean.DataSource;
import com.qeegoo.b2bautozityp.moudle.detail.bean.DetailBean;
import com.qeegoo.b2bautozityp.moudle.detail.bean.NetHelper;
import com.qeegoo.b2bautozityp.moudle.detail.bean.WareHouseBean;
import com.qeegoo.b2bautozityp.utils.TimeUtils;

import base.lib.util.ActivityManager;
import retrofit2.Call;

public class OrderDetailsActivity extends FragmentActivity implements View.OnClickListener, DataSource.CallBack<DetailBean>, View.OnDragListener {

    public static final String FRAGMENT_CATEGORY_TYPE = "fragmentType";
    public static final String ID_GOODS = "id_goods";

    public static final int LOOKFOR_PRICE_DETAIL = 1;
    public static final int GOTO_PRICE_DETAIL = 2;
    private int fragmentType;
    private TextView tv_title;
    private FrameLayout fl_container;
    private TextView tv_vin;
    private TextView tv_inquryDate;
    private TextView tv_receipt;
    private ImageView iv_car_logo;
    private TextView tv_desc;
    private TextView tv_address;
    private ImageButton ib_back;
    public ListView slide_view;

    private android.support.v4.app.Fragment mCurrentFragment;
    private String askPriceOrderId;
    public DrawerLayout drawer;
    private Call<HttpResult<WareHouseBean>> call;
    private SlideApapter slideApapter = new SlideApapter();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_details);
        ActivityManager.setCurrentActivity(this);
        initArges();
        initWidget();
        initData();

    }


    private void initArges() {
        Bundle extras = getIntent().getExtras();
        fragmentType = extras.getInt(FRAGMENT_CATEGORY_TYPE);
        askPriceOrderId = extras.getString(ID_GOODS, "");
        if (LOOKFOR_PRICE_DETAIL == fragmentType) {
            mCurrentFragment = new LookForDetailFragment();
        } else {
            mCurrentFragment = new InqueryFragment();
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.fl_container, mCurrentFragment).commit();

    }

    private void initData() {

        if (fragmentType == LOOKFOR_PRICE_DETAIL) {
            tv_title.setText(getResources().getString(R.string.price_detail));
            NetHelper.getShowDetailData(askPriceOrderId, this);
        } else {
            tv_title.setText(getResources().getString(R.string.price_goto));
            NetHelper.getQuotedPriceAdd(askPriceOrderId, this);
        }
    }

    @Override
    public void onDataLoaded(DetailBean detailBean) {
        setCommonViewData(detailBean);
        setFragmentData(detailBean);
    }

    private void setFragmentData(DetailBean bean) {
        DataSet set = (DataSet) this.mCurrentFragment;
        set.setData(bean.getGoodsInfoMoreList());
    }


    private void initWidget() {
        ib_back = (ImageButton) findViewById(R.id.ib_back);
        drawer = (DrawerLayout) findViewById(R.id.drawer);
        tv_title = (TextView) findViewById(R.id.tv_title);
        fl_container = (FrameLayout) findViewById(R.id.fl_container);
        tv_vin = (TextView) findViewById(R.id.tv_vin);
        tv_inquryDate = (TextView) findViewById(R.id.tv_inquryDate);
        tv_receipt = (TextView) findViewById(R.id.tv_receipt);
        iv_car_logo = (ImageView) findViewById(R.id.iv_car_logo);
        tv_desc = (TextView) findViewById(R.id.tv_desc);
        tv_address = (TextView) findViewById(R.id.tv_address);
        slide_view = (ListView) findViewById(R.id.slide_view);
        ib_back.setOnClickListener(this);
        drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        drawer.setOnDragListener(this);
        slide_view.setAdapter(slideApapter);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.ib_back) {
            finish();
        }
    }





    @Override
    public void onDataNotAvaiable(String str) {
    }


    private void setCommonViewData(DetailBean bean) {
        tv_vin.setText(getResources().getString(R.string.price_vin_id) + bean.getVin());
        tv_inquryDate.setText(getResources().
                getString(R.string.price_inquery_date) + TimeUtils.dateToString(bean.getUpdateTime()));
        Glide.with(this).load(bean.getCarLogoUrl()).centerCrop().into(iv_car_logo);
        tv_receipt.setText(bean.getScopeStatus() == 0 ? getResources().getString(R.string.price_receipt_no) : getResources().getString(R.string.price_receipt_yes));
        tv_desc.setText(bean.getCarModelName());
        tv_address.setText(bean.getAddress());
    }

    @Override
    public boolean onDrag(View view, DragEvent dragEvent) {
        return false;
    }


    public LeftAdapter getLeftAdapter(){
        return ((InqueryFragment)mCurrentFragment).getLeftAdapter();
    }
}
